<?php
require_once(Config::acquire('Model', null, 'model'));
require_once(Config::acquire('Netwerk', MODEL));
require_once(Config::acquire('Hardware', MODEL));
/**
 * Created by JetBrains PhpStorm.
 * User: Sander
 * Date: 29-6-11
 * Time: 17:11
 * To change this template use File | Settings | File Templates.
 */

class GeinstalleerdeHardware extends Model
{
    private $netwerk;
    private $hardware = array();

    public static function read($id)
    {
        $hardware = array();
        $list = SQL::query("SELECT * FROM `netwerk_hw` WHERE `netwerk_ID` = $id");
        while ($list->isValid()) {
            $hardware[] = Hardware::read($list->fetchField('hardware_ID'));
            $list->next();
        }
        return GeinstalleerdeHardware::create(Netwerk::read($id), $hardware);
    }

    public function __construct(Netwerk $netwerk, $hardware)
    {
        $this->netwerk = $netwerk;
        $this->hardware = $hardware;
    }

    public static function create(Netwerk $netwerk, $hardware)
    {

        return new GeinstalleerdeHardware($netwerk, $hardware);
    }

    public function delete($hardwareID = null)
    {
        if (!is_null($hardwareID)) {
            SQL::execute("DELETE FROM `netwerk_hw` WHERE `Netwerk_ID` = '" . $this->netwerk->getId() . "' AND `hardware_ID` = '$hardwareID'");
        }
    }

    public function add($hardwareID)
    {
        SQL::execute("INSERT INTO `netwerk_hw` (`Netwerk_ID`, `hardware_ID`) VALUES ({$this->netwerk->getId()}, $hardwareID)");
    }

    public function toArray()
    {

    }

    public function setHardware($hardware)
    {
        $this->hardware = $hardware;
    }

    public function getHardware()
    {
        return $this->hardware;
    }

    public function update()
    {
        return;
    }


    public function getUnique()
    {
        $list = array();
        foreach (Hardware::readAll() as $hardware) {
            $list[$hardware->getID()] = $hardware->getNaam();
        }
        $hw = SQL::query("SELECT * FROM `netwerk_hw`");
        while ($hw->isValid()) {
            unset($list[$hw->fetchField('hardware_ID')]);
            $hw->next();
        }

        return $list;
    }
}